package cn.lbf.stuniversity.equipment.service;


import cn.lbf.stuniversity.constant.Code;
import cn.lbf.stuniversity.equipment.dao.EquipmentDao;
import cn.lbf.stuniversity.equipment.entity.Equipment;
import cn.lbf.stuniversity.equipment.form.EquipmentAddForm;
import cn.lbf.stuniversity.equipment.form.EquipmentDetailForm;
import cn.lbf.stuniversity.equipment.form.EquipmentForm;
import cn.lbf.stuniversity.utils.PageUtil;
import cn.lbf.stuniversity.utils.ResultUtil;
import cn.lbf.stuniversity.vo.PageVO;
import cn.lbf.stuniversity.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EquipmentServiceImpl implements EquipmentService{
    @Autowired
    private EquipmentDao equipmentDao;
    @Override
    public Result<Map<String, Object>> addNew(EquipmentAddForm form) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("equipmentId",form.getEquipmentId());
        int EquipCount=equipmentDao.selectEquipmentCountByIdAndName(map);
        if(EquipCount>0){//如果查詢出來有同名的设备
            return new ResultUtil<Map<String, Object>>().setErrorMsg(Code.HAS_EXISTS, "已有同ID的设备!");
        }
        map = new HashMap<String, Object>();
        map.put("equipmentBasicId",form.getEquipmentBasicId());
        EquipCount=equipmentDao.selectEquipmentCountByIdAndName(map);
        if(EquipCount<0){//如果查詢没有该设备基础 ID下的设备
            return new ResultUtil<Map<String, Object>>().setErrorMsg(Code.HAS_EXISTS, "没有这个设备基础 ID的分类!");
        }
        int i = equipmentDao.addNew(form);
        if(i>0){
            return new ResultUtil<Map<String, Object>>().setSuccessMsg("添加成功");
        }else{
            return new ResultUtil<Map<String, Object>>().setErrorMsg(Code.FAIL, "添加失败，请联系管理员!");
        }
    }

    @Override
    public Result<Page<Equipment>> queryList(EquipmentForm form) {
        try{
            form.initOffSet();//
            List<Equipment> list = equipmentDao.selectEquipmentByIdAndName(form);
            long count = equipmentDao.queryCount(form);
            PageVO pageVO = new PageVO(form.getPageNumber(), form.getPageSize());
            PageImpl<Equipment> page = new PageImpl<>(list, PageUtil.initPage(pageVO), count);
            return new ResultUtil<Page<Equipment>>().setData(page);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<Page<Equipment>>().setErrorMsg("查询有误 请联系管理员");
        }
    }

    @Override
    public Result<String> del(String id) {
        Equipment quipment = equipmentDao.selectById(id);
        if(quipment==null){
            return new ResultUtil<String>().setErrorMsg(Code.NOT_EXISTS, "设备不存在");
        }
        int remove = equipmentDao.deleteByUpdateId(id);
        if (remove > 0) {
            return new ResultUtil<String>().setSuccessMsg("删除成功");
        }else {
            return new ResultUtil<String>().setErrorMsg(Code.FAIL,"删除失败 请联系管理员");
        }
    }

    @Override
    public Result<String> updateById(EquipmentDetailForm form) {
        Equipment equipment = equipmentDao.selectById(form.getEquipmentId());
        if(equipment ==null){
            return new ResultUtil<String>().setErrorMsg(Code.FAIL,"更新失败 查无此教室");
        }
        int updateNum = equipmentDao.updateByEquipmentId(form);
        if (updateNum > 0) {
            return new ResultUtil<String>().setSuccessMsg("更新成功!");
        }
        return new ResultUtil<String>().setErrorMsg("更新失败!");
    }
}
